package com.beilang.hotline.user.manager.impl;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.Constant;
import com.beilang.base.util.ReportUtil;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.dept.manager.DeptMgr;
import com.beilang.hotline.user.dao.UserDAO;
import com.beilang.hotline.user.manager.UserMgr;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.user.to.SearchTO;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import java.io.File;
import java.util.*;

public class UserMgrImpl implements UserMgr {
    private UserDAO userDAO;

    private CodeLookupMgr codeLookupMgr;

    private DeptMgr deptMgr;

    public void setDeptMgr(DeptMgr deptMgr) {
        this.deptMgr = deptMgr;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public UserModel createUser(UserModel userModel) {
        return userDAO.createUser(userModel);
    }

    public UserModel saveUser(UserModel userModel) {
        return userDAO.saveUser(userModel);
    }

    public void deleteUser(String userId) {
        userDAO.deleteUser(userId);
    }

    public UserModel getUser(String userId) {
        UserModel userModel = userDAO.getUser(userId);
        userModel = setAllDesc(userModel);
        return userModel;
    }

    public PaginationHelper getUserList(PaginationHelper helper, SearchTO searchTO) {
        int count = userDAO.searchCount(searchTO);
        helper.setTotalSize(count);
        List list = userDAO.searchList(helper, searchTO);
        if (list != null && list.size() > 0) {
            List l = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                UserModel userModel = (UserModel) list.get(i);
                userModel.setUserRoleDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_USER_ROLE_LIST, userModel.getUserRole()));
                userModel=setAllDesc(userModel);
                l.add(userModel);
            }
            helper.setList(l);
        } else {
            helper.setList(list);
        }
        helper.setList(list);
        return helper;
    }

    private UserModel setAllDesc(UserModel userModel) {
        if (userModel != null) {
            userModel.setHandlerIndStr(userModel.getHandlerIndStr());
            if (!StringUtil.isNullOrBlank(userModel.getRecordMode())) {
                userModel.setRecordModeDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_RECORD_MODE_LIST, userModel.getRecordMode()));
            }
            userModel.setUserRoleDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_USER_ROLE_LIST, userModel.getUserRole()));
            if (userModel.getDeptId() != null) {
                userModel.setDeptName(deptMgr.getDept(Long.parseLong(userModel.getDeptId())).getDeptName());
            }
            if (!StringUtil.isNullOrBlank(userModel.getPopupInd())) {
                userModel.setPopupIndDesc(codeLookupMgr.getCodeDesc(CodeConstant.IND_LIST, userModel.getPopupInd()));
            }
            if (!StringUtil.isNullOrBlank(userModel.getHandlerIndStr())) {
                userModel.setHandlerIndDesc(codeLookupMgr.getCodeDesc(CodeConstant.IND_LIST, userModel.getHandlerIndStr()));
            }
        }
        return userModel;
    }

    public List getUserListByChannel(String channelNo) {
        return userDAO.getUserListByChannel(channelNo);
    }

    public byte[] genereateUserReport(SearchTO searchTO) {
        byte[] buf = new byte[0];
        String fileName = "";
        try {
            fileName = getRandomFileName();
            File file = new File(Constant.FILEPATH + fileName);

            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
            jxl.write.WritableSheet ws = wwb.createSheet("用户", 0);
            writeUserExcelHeader(ws);
            List list = userDAO.searchList(searchTO);

            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int row = 1;
                while (itor.hasNext()) {
                    UserModel userModel = (UserModel) itor.next();
                    writeUserExcelDetail(userModel, ws, row);
                    row++;
                }
            }
            formatSheet(ws);

            wwb.write();
            wwb.close();
            ReportUtil reportUtil = new ReportUtil();
            buf = reportUtil.readByteArrayOfSpecifiedFile(Constant.FILEPATH + fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buf;
    }

    public void writeUserExcelDetail(UserModel userModel, jxl.write.WritableSheet ws, int row) {
        try {
            userModel = setAllDesc(userModel);
            jxl.write.Label label1 = new jxl.write.Label(0, row, String.valueOf(row), getCell(false));
            jxl.write.Label label2 = new jxl.write.Label(1, row, StringUtil.deNull(userModel.getUserId()), getCell(false));
            jxl.write.Label label3 = new jxl.write.Label(2, row, StringUtil.deNull(userModel.getUserName()), getCell(false));
            jxl.write.Label label4 = new jxl.write.Label(3, row, StringUtil.deNull(userModel.getDeptName()), getCell(false));
            jxl.write.Label label5 = new jxl.write.Label(4, row, StringUtil.deNull(userModel.getUserRoleDesc()), getCell(false));
            jxl.write.Label label6 = new jxl.write.Label(5, row, StringUtil.deNull(userModel.getPcName()), getCell(false));
            jxl.write.Label label7 = new jxl.write.Label(6, row, StringUtil.deNull(userModel.getChannelNo()), getCell(false));
            jxl.write.Label label8 = new jxl.write.Label(7, row, StringUtil.deNull(userModel.getRecordModeDesc()), getCell(false));
            jxl.write.Label label9 = new jxl.write.Label(8, row, StringUtil.deNull(userModel.getEmailAddress()), getCell(false));
            jxl.write.Label label10 = new jxl.write.Label(9, row, StringUtil.deNull(userModel.getTelephone()), getCell(false));

            String result = "";
            List list = userModel.getAccessCompanyList();
            int i = 0;
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                while (itor.hasNext()) {
                    String temp = (String) itor.next();
                    if (i == 0) {
                        result = codeLookupMgr.getCodeDesc(CodeConstant.CODE_COMPANY_TYPE_LIST, temp);
                    } else {
                        result = result + "," + codeLookupMgr.getCodeDesc(CodeConstant.CODE_COMPANY_TYPE_LIST, temp);
                    }
                    i++;
                }
            }
            jxl.write.Label label11 = new jxl.write.Label(9, row, StringUtil.deNull(result), getCell(false));
            ws.addCell(label1);
            ws.addCell(label2);
            ws.addCell(label3);
            ws.addCell(label4);
            ws.addCell(label5);
            ws.addCell(label6);
            ws.addCell(label7);
            ws.addCell(label8);
            ws.addCell(label9);
            ws.addCell(label10);
            ws.addCell(label11);
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

    public static void formatSheet(jxl.write.WritableSheet ws) {
        for (int i = 0; i < 11; i++) {
            if (i == 0) {
                ws.setColumnView(i, 10);
            } else {
                ws.setColumnView(i, 20);
            }
        }
    }

    public void writeUserExcelHeader(jxl.write.WritableSheet ws) {
        try {
            jxl.write.Label label1 = new jxl.write.Label(0, 0, "序号", getCell(true));
            jxl.write.Label label2 = new jxl.write.Label(1, 0, "用户名", getCell(true));
            jxl.write.Label label3 = new jxl.write.Label(2, 0, "姓名", getCell(true));
            jxl.write.Label label4 = new jxl.write.Label(3, 0, "部门", getCell(true));
            jxl.write.Label label5 = new jxl.write.Label(4, 0, "角色", getCell(true));
            jxl.write.Label label6 = new jxl.write.Label(5, 0, "弹屏电脑", getCell(true));
            jxl.write.Label label7 = new jxl.write.Label(6, 0, "录音通道", getCell(true));
            jxl.write.Label label8 = new jxl.write.Label(7, 0, "录音模式", getCell(true));
            jxl.write.Label label9 = new jxl.write.Label(8, 0, "邮件", getCell(true));
            jxl.write.Label label10 = new jxl.write.Label(9, 0, "联系电话", getCell(true));
            jxl.write.Label label11 = new jxl.write.Label(10, 0, "企业类型", getCell(true));

            ws.addCell(label1);
            ws.addCell(label2);
            ws.addCell(label3);
            ws.addCell(label4);
            ws.addCell(label5);
            ws.addCell(label6);
            ws.addCell(label7);
            ws.addCell(label8);
            ws.addCell(label9);
            ws.addCell(label10);
            ws.addCell(label11);
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

    public String getRandomFileName() {
        Date cur = new Date();
        Calendar cld = Calendar.getInstance();
        cld.setTime(cur);
        int mth = cld.get(Calendar.MONTH) + 1;
        String nowStr = cld.get(Calendar.YEAR) + "" + mth + cld.get(Calendar.DATE) + cld.get(Calendar.HOUR) + cld.get(Calendar.MINUTE) + cld.get(Calendar.SECOND);
        Random rdm = new Random();
        String rdmNum = String.valueOf(rdm.nextInt());
        return nowStr + rdmNum + Constant.EXCEL_EXTENSION;
    }

    public WritableCellFormat getCell(boolean isBold) {

        WritableFont font = null;
        if (isBold) {
            font = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
        } else {
            font = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD);
        }


        try {
            font.setColour(Colour.BLACK);
        } catch (WriteException e1) {
            e1.printStackTrace();
        }
        WritableCellFormat format = new WritableCellFormat(font);
        try {
            format.setAlignment(jxl.format.Alignment.LEFT);
            format.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return format;
    }


}
